package com.example.myapplication;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.example.myapplication.bean.NewsListBean;
import com.example.myapplication.databinding.ActivityNewsInitBinding;
import com.example.myapplication.util.Network;
import com.example.myapplication.util.TagUtil;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;


public class NewsInitActivity extends AppCompatActivity {
    private final String TAG = "NewsInitActivity";
    private ActivityNewsInitBinding viewBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        viewBinding = ActivityNewsInitBinding.inflate(getLayoutInflater());
        setContentView(viewBinding.getRoot());

        /* 显示App icon左侧的back键 */
        setBar();
        initData();
    }

    private void setBar() {
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null){
            actionBar.setTitle(R.string.news_init);
            //决定左上角的图标是否可以点击
            actionBar.setHomeButtonEnabled(true);
            // 给左上角图标的左边加上一个返回的图标
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish(); //关闭当前页面
                return true;
        }
        return super.onOptionsItemSelected(item);
    }


    private void initData() {
        //获取新闻ID
        Intent intent = getIntent();
        String newsId = intent.getStringExtra(TagUtil.NEWS_ID);
        Log.d(TAG, "initData:newsId= " + newsId);

        Network.doGet(NewsInitActivity.this,"content?uniquekey=" + newsId + "&key=", new Network.OkResult() {
            @Override
            public void success(JSONObject jsonObject) {
                JSONObject resultObject = null;
                try {
                    resultObject = jsonObject.getJSONObject("result");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                String dataJson = resultObject.optString("detail");

                NewsListBean homeList1Beans = new Gson().fromJson(dataJson, NewsListBean.class);
                if (homeList1Beans != null){
                    viewBinding.tvNewsInitTitle.setText(homeList1Beans.getTitle());
                    viewBinding.tvNewsInitFrom.setText("来源：" + homeList1Beans.getAuthor_name());
                    viewBinding.tvNewsInitDate.setText( homeList1Beans.getDate());
                    if (!TextUtils.isEmpty(homeList1Beans.getThumbnail_pic_s())){
                        Glide.with(viewBinding.imgNewsInitImg)
                                .load(homeList1Beans.getThumbnail_pic_s())
                                .into(viewBinding.imgNewsInitImg);
                    }else{
                        viewBinding.imgNewsInitImg.setVisibility(View.GONE);
                    }
                    viewBinding.web.loadData(resultObject.optString("content"), "text/html", "UTF-8");
                }else{
                    Log.d(TAG, "success: 新闻详情数据为空");
                }


            }

            @Override
            public void error(String message) {

            }
        });



}

    @Override
    protected void onDestroy() {
        //停止加载
        viewBinding.web.stopLoading();

        //清空回调
        viewBinding.web.setWebViewClient(null);
        viewBinding.web.setWebChromeClient(null);

        //从父容器移除
        ViewGroup parent = (ViewGroup) viewBinding.web.getParent();
        if (parent != null) {
            parent.removeView(viewBinding.web);
        }

        // 销毁实例
        viewBinding.web.destroy();


        super.onDestroy();
    }
}